// Quote.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

#include <iostream>
#include <string>
#include <utility>

/** 可用于在函数调用中实现按引用传递语义： [8/25/2020 ZC] */
void double_string(std::string& s) {
	s += s; // 's' 与 main() 的 'str' 是同一对象
}

/** 当函数的返回值是左值引用时，函数调用表达式成为左值表达式 [8/25/2020 ZC] */
char& char_number(std::string& s, std::size_t n) {
	return s.at(n); // string::at() 返回 char 的引用
}

void f(int& x) {
	std::cout << "lvalue reference overload f(" << x << ")\n";
}

void f(const int& x) {
	std::cout << "lvalue reference to const overload f(" << x << ")\n";
}

void f(int&& x) {
	std::cout << "rvalue reference overload f(" << x << ")\n";
}

int main() {
	std::string str = "Test";
	//double_string(str);
	//////////////////////////////////////////////////////////////////////////
	//char_number(str, 1) = 'a';/** 函数调用是左值，可以被赋值 [8/25/2020 ZC] */
	//char& t = char_number(str, 2);
	//t = 'a';
	//////////////////////////////////////////////////////////////////////////
	const std::string& r2 = str + str;/** 到const的左值引用延长生存期 [8/25/2020 ZC] */
	//r2 += "Test";/** 不能通过const引用修改 [8/25/2020 ZC] */

	std::string&& r3 = str + str;/** 右值引用延长生存期 [8/25/2020 ZC] */
	r3 += "Test";

	std::cout << r3 << '\n';
}

// Run program: Ctrl + F5 or Debug > Start Without Debugging menu
// Debug program: F5 or Debug > Start Debugging menu

// Tips for Getting Started: 
//   1. Use the Solution Explorer window to add/manage files
//   2. Use the Team Explorer window to connect to source control
//   3. Use the Output window to see build output and other messages
//   4. Use the Error List window to view errors
//   5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
//   6. In the future, to open this project again, go to File > Open > Project and select the .sln file
